(function () {
	
    //人员table数据显示
    var serverData = {};
    var	serverTable;
    //详情显示
    var serverData2 = {};
    serverData2.beginDate = '';
    serverData2.endDate = '';
    serverData2.cyc = 'week';
    serverData2.uId = '';
    serverData2.dataType = '';
	serverData2.dataId = '';
    var	serverTable2;
    
    function viewDetails(o){
    		window.location = window.rootPath + '/ui/eventRegister?eventReportIdentify='+o.registerEventToReportEvent.id+"&eventRegisterIdentify="+o.id;
    }
    
    jQuery(document).ready(function () {
        serverTable=  jQuery('#dataTable').DataTable({
            "serverSide": true,
            "ordering": false,
            "ajax": {
                "dataType": "json",
                "type": "post",
                "cache": false,
                "url": window.rootPath+'/ui/ScoreAnalysis/getUserPage',
                "data": function (d) {
                    return $.extend({}, d, serverData);
                }
            },
            columns: [{
	                data: null,
	                defaultContent: ""
        			},
                {
        				data: 'userName'
                },
                {
                		data: 'position'
                },
                {
                		data: 'post'
                },
                {
                		data: 'hierarchy'
                },
                {
                		data: 'grading'
                },
                {
                		data: 'scale'
                }],
            "drawCallback": function ( oSettings ) {
                var k = 0;
                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

                for ( var i=oSettings._iDisplayStart, iLen=total ; i<iLen ; i++ )
                {
                    $('td:eq(0)', oSettings.aoData[ oSettings.aiDisplay[k] ].nTr ).text( i+1 );
                    k++;
                }

                serverTable.off('click','tr');
                serverTable.on( 'click', 'tr', function (event) {
                    event=event||window.event;
                    var target=event.target||event.scrElement;
                    var data = serverTable.row( this ).data();
                    //监听点击
                    getAnalysisData(data.id);
                    return;
                } );
            }
        });
        
        serverTable2=  jQuery('#dataTable2').DataTable({
	        	"serverSide": true,
	        	"ordering": false,
	        	"ajax": {
	        		"dataType": "json",
	        		"type": "post",
	        		"cache": false,
	        		"url": window.rootPath+'/ui/ScoreAnalysis/getEvaluatePage',
	        		"data": function (d) {
	        			return $.extend({}, d, serverData2);
	        		}
	        	},
	        	columns: [{
	        		data: null,
	        		defaultContent: ""
	        	},
	        	{
                data: null,
                defaultContent: "",
                render: function (data, type, full, meta) {
                		var operate ='<button type="button" class="btn btn-sm btn-info detailBtn" title="详情"><i class="fa fa-edit"></i></button>';
                		return operate;
                }
	        	},
	        	{
	        		data: 'registerEvent.registerEventName'
	        	},
	        	{
	        		data: 'registerEvent.registerEventType.eventName'
	        	},
	        	{
	        		data: 'registerEvent.registerEventReportTime',
                render: function (data, type, full, meta) {
                    if (data)
                        return moment(data).format('YYYY/MM/DD HH:mm');
                    else
                        return '';
                }
	        	},
	        	{
	        		data: 'registerEvent.registerEventHierarchy.groupName'
	        	},
	        	{
	        		data: 'registerEvent.registerEventToNetGrid.gridName'
	        	},
	        	{
	        		data: 'registerEvent.registerEventToNetGrid.division.divisionName'
	        	},
	        	{
	        		data: 'registerEvent.registerEventToPartBaseInfo.partName'
	        	},
	        	{
	        		data: 'registerEventEvaluateLevel.gradingName'
	        	},
	        	{
	        		data: 'scale'
	        	}],
	        	"drawCallback": function ( oSettings ) {
	        		var k = 0;
	        		var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;
	        		
	        		for ( var i=oSettings._iDisplayStart, iLen=total ; i<iLen ; i++ )
	        		{
	        			$('td:eq(0)', oSettings.aoData[ oSettings.aiDisplay[k] ].nTr ).text( i+1 );
	        			k++;
	        		}
	        		
	        		serverTable2.off('click','tr');
	        		serverTable2.on( 'click', 'tr', function (event) {
	        			event=event||window.event;
	        			var target=event.target||event.scrElement;
	        			var data = serverTable2.row( this ).data();
	        			if (target.className.indexOf('fa-edit') != -1 ||target.className.indexOf('detailBtn') != -1) {
	        				viewDetails(data.registerEvent);
	        			}
	        			return;
	        		} );
	        	}
        });
        
        //人员查询
        jQuery('.searchBtn').on('click',function(){
            serverData.key=jQuery('#search').val();
            serverTable.draw();
        })
        
        //人员撤销查询
        jQuery('.resetBtn').on("click",function(){
            jQuery('#search').val('');
            serverData.key ='';
            serverTable.draw();
        });
        
        //详情查询
        jQuery('.searchBtn2').on('click',function(){
	        	serverData2.key=jQuery('#search2').val();
	        	serverTable2.draw();
        })
        
        //详情撤销查询
        jQuery('.resetBtn2').on("click",function(){
	        	jQuery('#search2').val('');
	        	serverData2.key ='';
	        	serverTable2.draw();
        });
        
        //周期切换
        jQuery('#cyc').on('change',function(){
        		var cyc = $(this).val();
        		var cycDate = $('#cycDate');
        		serverData2.cyc = cyc;
        		if(cyc == 'custom'){
        			cycDate.show();
        		}else{
        			cycDate.hide();
        			//serverTable.draw(false);
        			getAnalysisData(serverData2.uId);
        			serverTable2.draw(false);
        		}
        })
        
        //部件监听
        pie1.on('click', function (params) {
    			serverData2.dataType = params.seriesName;
    			serverData2.dataId = params.data.id;
    			serverTable2.draw(false);
        });
        
        //事件监听
        pie2.on('click', function (params) {
	        	serverData2.dataType = params.seriesName;
	        	serverData2.dataId = params.data.id;
	        	serverTable2.draw(false);
        });
        
        //区域监听
        pie3.on('click', function (params) {
        		serverData2.dataType = params.seriesName;
        		serverData2.dataId = params.data.id;
        		serverTable2.draw(false);
        });
        
        jQuery('.cycBtn').on("click",function(){
        		var beginDate = $('#beginDate').val();
        		var endDate = $('#endDate').val();
        		if(beginDate == '' || endDate == ''){
        			alert("请选择周期时间");
        			return;
        		}
        	    serverData2.beginDate = beginDate;
        	    serverData2.endDate = endDate;
        	    
        	    getAnalysisData(serverData2.uId);
        	    
            //serverTable.draw(false);
            serverTable2.draw(false);
        });
        
    });
    
    function getAnalysisData(id){
    		var d = {
    				'cyc' : $('#cyc').val(),
    				'beginDate' : $('#beginDate').val(),
    				'endDate' : $('#endDate').val()
    			}
    		serverData2.uId = id;
	    jQuery.ajax({
	        url:window.rootPath+'/ui/ScoreAnalysis/getAnalysisData/'+id,
	        cache:false,
	        type :"post",
	        data :JSON.stringify(d),
	        dataType:"json",
	        contentType:'application/json;charset=utf-8',//解决返回数据中的中文乱码问题
	        success:function(data){
	        		var part = data.part;
	        		updatePieForPart(part,pie1);
	        		var event = data.event;
	        		updatePie(event,pie2);
	        		var grid = data.grid;
	        		var division = data.division;
	        		updatePieForGrid(division,grid,pie3);
	        }
	    });
    };
    
    
    function updatePie(data,pie){
    		pie.setOption({
    		//	legend:{ data : data.legend },
			series:{ data : data.series }
		});
    }
    
    function updatePieForPart(data,pie){
	    	pie.setOption({
	    	//	legend:{ data : data.legend },
	    		series:[{ data : data.seriesIn },{data : data.seriesOut}]
	    	});
    }
    
    function updatePieForGrid(division,grid,pie){
    		pie.setOption({
    		//	legend:{ data : data.legend },
    			series:[{ data : division.series },{data : grid.series}]
    		});
    }
    
    /**
     * 按部件进行分类
     */
    var pie1;
    pie1 = echarts.init(document.getElementById('pie1'));
    var option1 = {
    	    title: {
    	        text: '部件',
    	        x: 'center',
    	        y: 'center',
    	        textStyle: {
    	            fontWeight: 'normal',
    	            fontSize: 22
    	        }
    	    },
    	    tooltip: {
    	        trigger: 'item',
    	        formatter: function(params, ticket, callback) {
    	        		var part = '';
    	        		if(params.name == "非部件"){
    	        			part = '非部件';
    	        		}else{
    	        			part = '部件';
    	        		}
    	            var res = part +'</br>' 
    	            		+ params.name + ' : ' + params.value + '</br>' 
    	            		+ params.percent +'%';
    	            return res;
    	        }
    	    },
    	    legend: {
    	        orient: 'horizontal',
    	        x: 'center',
    	        y: 'top',
    	        right: '0%',
    	        bottom: '0%',
    	        data: '',
    	        itemWidth: 40,
    	        itemHeight: 30
    	    },
    	    series: [
    	    	 {
    	             name:'part',
    	             type:'pie',
    	             selectedMode: 'single',
    	             radius: [0, '45%'],
    	             label: {
    	                 normal: {
    	                     position: 'inner'
    	                 }
    	             },
    	             labelLine: {
    	                 normal: {
    	                     show: false
    	                 }
    	             },
    	             data:''
    	         },
    	         {
    	             name:'part',
    	             type:'pie',
    	             radius: ['55%', '90%'],
				 label: {
					normal: {
						position: 'inner',
						textStyle: {
							color: '#fff',
							fontSize: 14
						}
					}
				 },
				 labelLine: {
					normal: {
						show: false
					}
				 },
    	             data:''
    	         }
    	    ]
    	};
    pie1.setOption(option1);
    
    /**
     * 按事件进行分类
     */
    var pie2;
    pie2 = echarts.init(document.getElementById('pie2'));
    var option2 = {
    		title: {
    			text: '事件类型',
    			x: 'center',
    			y: 'center',
    			textStyle: {
    				fontWeight: 'normal',
    				fontSize: 22
    			}
    		},
    		tooltip: {
    			trigger: 'item',
    			formatter: function(params, ticket, callback) {
    				var res =  params.name + ' : ' + params.value + '<br>' 
    				+ params.percent +'%';
    				return res;
    			}
    		},
    		legend: {
    			orient: 'horizontal',
    			x: 'center',
    			y: 'top',
    			right: '0%',
    			bottom: '0%',
    			data: '',
    			itemWidth: 40,
    			itemHeight: 30
    		},
    		series: [{
    			name: 'event',
    			type: 'pie',
    			selectedMode: 'single',
    			radius: ['50%', '90%'],
    			label: {
    				normal: {
    					position: 'inner',
    					textStyle: {
    						color: '#fff',
    						fontSize: 14
    					}
    				}
    			},
    			labelLine: {
    				normal: {
    					show: false
    				}
    			},
    			data: ''
    		}]
    };
    pie2.setOption(option2);
    
    /**
     * 按网格 进行分类
     */
    var pie3;
    pie3 = echarts.init(document.getElementById('pie3'));
    var option3 = {
    		title: {
    			text: '网格',
    			x: 'center',
    			y: 'center',
    			textStyle: {
    				fontWeight: 'normal',
    				fontSize: 22
    			}
    		},
    		tooltip: {
    			trigger: 'item',
    			formatter: function(params, ticket, callback) {
    				 var res =  params.name + ' : ' +  params.percent +'%<br>' 
     	            + params.value;
    				return res;
    			}
    		},
    		legend: {
    			orient: 'horizontal',
    			x: 'center',
    			y: 'top',
    			right: '0%',
    			bottom: '0%',
    			data: '',
    			itemWidth: 40,
    			itemHeight: 30
    		},
    		series: [
   	    	 	{
	             name:'division',
	             type:'pie',
	             selectedMode: 'single',
	             radius: [0, '45%'],
	             label: {
	                 normal: {
	                     position: 'inner'
	                 }
	             },
	             labelLine: {
	                 normal: {
	                     show: false
	                 }
	             },
	             data:''
	         },
	         {
	             name:'grid',
	             type:'pie',
	             radius: ['55%', '90%'],
			 label: {
				normal: {
					position: 'inner',
					textStyle: {
						color: '#fff',
						fontSize: 14
					}
				}
			 },
			 labelLine: {
				normal: {
					show: false
				}
			 },
	             data:''
	         }
	    ]
    };
    pie3.setOption(option3);
    //网格  部件（非部件） 事件类型
    
})();